import React from 'react';
import {Form ,Row ,Col, Select ,Button,Modal} from 'antd';
import Grid from '../../components/table/grid';
import {injectIntl} from 'react-intl';
import {getYear} from '../../utils/Array';
import moment from 'moment';
import axios from "axios/index";
import {sessionManager} from "../../utils/sessionStorage";
import qs from 'qs';

const FormItem = Form.Item;
const createForm = Form.create;
const Option = Select.Option;
const confirm = Modal.confirm;
// let RowRecord = this.props.location.state.rowRecord;
// console.log('RowRecord',RowRecord)
class merchantSettlementDetail extends React.Component {
	constructor(props) {
		super(props);
		this.common = this.props.intl.messages.common;
		this.state={
			params:{

			},
			refresh:0
		};
		this.columns = [
			{
				title: '结算日期',
				dataIndex: 'settlementDay',
				key: 'settlementDay',
			},
			{
				title: '电量（KWH）',
				dataIndex: 'electric',
				key: 'electric',
			},
			{
				title: '电费（元）',
				dataIndex: 'electricAmount',
				key: 'electricAmount',
			},
			{
				title: '服务费（元）',
				dataIndex: 'serviceAmount',
				key: 'serviceAmount',
			},
			{
				title: '总费用（元）',
				dataIndex: 'totalAmount',
				key: 'totalAmount',
			},
			{
				title: this.common.operation,
				key: 'action',
				render: (text, record) => (
					<a onClick={() => this.goLink(text,record,'merchantLedgerOrder')}>分账订单</a>
				)
			}
		]
	}

	componentWillMount() {
		let newParams;
		if(this.props.location.state&&this.props.location.state.rowRecord){
			newParams = {
				id:this.props.location.state.rowRecord.id,
				settlementUserId:this.props.location.state.rowRecord.settlementUserId,
				settlementDate:this.props.location.state.rowRecord.settlementDate,
				settleUserId:this.props.location.state.rowRecord.settleUserId,
				lastSettlementDate:this.props.location.state.rowRecord.lastSettlementDate
			}
		}else{
			newParams = {
				settlementUserId:sessionManager.get('rowRecord').settlementUserId||'',
				settlementDate:sessionManager.get('rowRecord').settlementDate||'',
				settleUserId:sessionManager.get('rowRecord').settleUserId||'',
				lastSettlementDate:sessionManager.get('rowRecord').lastSettlementDate||''
			}
		}
		this.setState(Object.assign(this.state.params,newParams));
		console.log('-----componentWillMount----',this.state.params)
	}
	componentWillUnmount() {

	}
	goLink=(text,record,link)=>{
		if(this.props.location.state&&this.props.location.state.rowRecord){
			sessionManager.set('rowRecord', this.props.location.state.rowRecord);
		}/*else{
			sessionManager.set('rowRecord',sessionManager.get('rowRecord'));
		}*/

		this.props.history.push({pathname: `/finance/${link}`,state: {orderRecord: text}});
	};


	handleSubmit = (e) => {
		e.preventDefault();
		this.props.form.validateFieldsAndScroll((err, values) => {
			if (!!err) {
				return;
			}
			console.log(values);
			this.setState(Object.assign(this.state.params,values));
			this.setState({
				refresh:this.state.refresh+1
			})
		});
	};


	render() {
		const token=JSON.parse(sessionStorage.getItem("token")||{}).token;
		let locale=sessionManager.get('locale');
		let exportQs=`${qs.stringify(this.state.params)}&token=${token}&locale=${locale}`;
		console.log('exportQs',exportQs);
		const {getFieldDecorator} = this.props.form;
		return (
			<div>
				{/*<form form={this.props.form} className="SC-form">
					<Row gutter={24} type="flex">
						<Col span={3}>
							<FormItem
							>
								{getFieldDecorator('year',{
									initialValue:this.state.params.year
								})(
									<Select placeholder={'年份'}>
										{getYear().map((k, i) => {
											return <Option value={k} key={i}>{k}</Option>
										})}
									</Select>
								)}
							</FormItem>
						</Col>
						<Col>
							<FormItem>
								<Button type="primary" htmlType="submit"
										onClick={this.handleSubmit} className="ant-btn-84">{this.common.search}</Button>
								<a href={`${axios.defaults.baseURL}/api/settlement/settled/monthlyBill/export?${exportQs}`}>
									<Button type="primary">{this.common.exportStatement}</Button>
								</a>
							</FormItem>
						</Col>
					</Row>
				</form>*/}
				<Grid columns={this.columns} params={this.state.params}
					  url='api/settlement/billSplit/settlementOrderBillDetail' refresh={this.state.refresh}/>
			</div>
		)
	}
}

merchantSettlementDetail = createForm()(merchantSettlementDetail);
export default injectIntl(merchantSettlementDetail)
